<?php
namespace Admin\Controller;

use Think\Controller;

class LinkController extends CommonController
{
    protected $Link;
    protected $links;

    function __construct()
    {
        parent::__construct();
        $this->Link = M('link');
    }

    public function link()
    {
        $links = $this->Link->order('sort_order')->select();
        $this->assign('links', $links);
        //dump($links);

        $this->display();
    }

    public function store()
    {
        if($this->Link->create()) {
            $this->Link->add();
            $this->success('新增链接成功');
        } else {
            $this->error('新增失败！');
        }

    }


    public function update()
    {
        if($this->Link->create()) {
            dump($_POST);
            //exit();
            $this->Link->save();
            $this->success('修改链接成功!');
        } else {
            $this->error('修改失败！');
        }
    }

    public function destory()
    {
        $id = I('get.id');


        if($this->Link->delete($id)) {
            $this->success('删除成功!');
        }else{
            $this->error('删除失败！');
        }
    }

    public function destroy_all(){
        $del_id = I('post.del_id');
        dump($del_id);
        dump($_POST);
        //exit();

        foreach ($del_id as $v){
            $this->Link->delete($v);
        }
        $this->success('删除成功！');
    }

    public function sort_order(){
        dump($_POST);
        $sort_id = I('post.sort_id');
        $sort_order = I('post.sort_order');

        foreach ($sort_id as $k=>$v){
            $this->Link->where("id=$v")->setField('sort_order',"$sort_order[$k]");
        }
        $this->redirect('link');

    }

}